package com.example.automobile_positioning.activity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.example.automobile_positioning.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class VideoShowActivity extends Activity {

	private ListView listView;
	private List<File> lists = new ArrayList<File>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_show);
		listView = (ListView) findViewById(R.id.list_video);
		File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Video/");
		if (!folder.exists()) {
			 folder.mkdirs();// 创建文件夹
		}
		File[] files = folder.listFiles();
		if (files!=null && files.length > 0) {
		    for(int i = 0; i < files.length; i++) {
		    	lists.add(files[i]);
			}
		} else {
			Toast.makeText(this, "没有找到视频", 0).show();
		}
		MyAdapter myAdapter = new MyAdapter();
		listView.setAdapter(myAdapter);
	}
	
	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lists.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return lists.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.activity_list_item, null);
				Viewholder viewholder = new Viewholder();
				viewholder.tv = (TextView) convertView.findViewById(R.id.item_tv);
				convertView.setTag(viewholder);
			}
			final Viewholder viewholder = (Viewholder) convertView.getTag();
			viewholder.tv.setText(lists.get(position).getName());
			viewholder.tv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.putExtra("oneshot", 0);
			        intent.putExtra("configchange", 0);
			        Uri uri = Uri.fromFile(lists.get(position));
					intent.setDataAndType(uri, "video/*");
					startActivity(intent);
				}
			});
			return convertView;
		}
	} 
	class Viewholder {
		TextView tv;
	}
}
